<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="4" :md="8">
            <el-form-item label="异常编号:">
              <el-input
                clearable
                v-model="where.operation_no"
                placeholder="请输入异常编号"/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="所属单位:">
              <organization-select v-model="where.organization_id" model_type="way"></organization-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="上报用户:">
              <el-input
                clearable
                v-model="where.create_user"
                placeholder="请输入上报用户"/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="作业类别:">
              <operation-category-select v-model="where.operation_category"></operation-category-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="巡查分类:">
              <exception-category-select type="way" :unit="where.organization_id" v-model="where.exception_category"></exception-category-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="是否单位可见:">
              <el-select
                clearable
                v-model="where.organization_show"
                placeholder="请选择单位是否可见"
                class="ele-fluid">
                <el-option label="可见" value="1"/>
                <el-option label="不可见" value="0"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <el-form-item label="是否处罚:">
              <el-select
                clearable
                v-model="where.penalty"
                placeholder="请选择是否处罚"
                class="ele-fluid">
                <el-option label="是" value="1"/>
                <el-option label="否" value="0"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="异常地址:">
              <el-cascader
                  clearable
                  v-model="where.area_id"
                  class="ele-fluid"
                  placeholder="请选择省市区"
                  :options="regions"
                  popper-class="ele-pop-wrap-higher"/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="8">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            @click="openEdit(null)"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            size="small">添加
          </el-button>
<!--          v-if="permission.includes('road-traffic:disease:add')"-->
          <el-button
              @click="setShowBatch"
              :disabled="!this.selection.length"
              type="primary"
              icon="el-icon-setting"
              class="ele-btn-icon"
              size="small">设置可见
          </el-button>
          <!--          v-if="permission.includes('road-traffic:disease:setShow')"-->
          <el-button
            @click="removeBatch"
            :disabled="!this.selection.length"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            size="small">删除
          </el-button>
<!--          v-if="permission.includes('road-traffic:disease:del')"-->

        </template>
        <template slot="organization_show" slot-scope="{row}">
          <el-tag
            :type="['warning', 'success'][row.organization_show]"
            size="mini">
            {{ ['否', '是'][row.organization_show] }}
          </el-tag>
        </template>
        <template slot="penalty" slot-scope="{row}">
          <el-tag
            :type="['warning', 'success'][row.penalty]"
            size="mini">
            {{ ['否', '是'][row.penalty] }}
          </el-tag>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:member:edit')">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:member:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { regionData } from 'element-china-area-data'
import organizationSelect from '@/components/organizationSelect';
import exceptionCategorySelect from './components/exceptionCategorySelect';
import operationCategorySelect from './components/operationCategorySelect';

export default {
  name: 'Disease',
  computed: {
    ...mapGetters(["permission"]),
  },
  components: {
    organizationSelect,
    exceptionCategorySelect,
    operationCategorySelect
  },
  data() {
    return {
      regions: regionData,
      url: '/disease/getList',
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'operation_no',
          label: '异常编号',
          align: 'center',
          showOverflowTooltip: true,
          width: 120,
        },
        {
          prop: 'organization_name',
          label: '所属单位',
          align: 'center',
          showOverflowTooltip: true,
          width: 100,
        },
        {
          prop: 'operation_category_name',
          label: '作业类别',
          align: 'center',
          showOverflowTooltip: true,
          width: 100
        },
        {
          prop: 'exception_category_name',
          label: '巡查分类',
          align: 'center',
          showOverflowTooltip: true,
          width: 100
        },
        {
          prop: 'area',
          label: '异常地址',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 120
        },
        {
          prop: 'organization_show',
          label: '单位可见',
          align: 'center',
          showOverflowTooltip: true,
          width: 100,
          slot: 'organization_show'
        },
        {
          prop: 'penalty',
          label: '是否处罚',
          align: 'center',
          showOverflowTooltip: true,
          width: 100,
          slot: 'penalty'
        },
        {
          prop: 'create_user',
          label: '上报用户',
          align: 'center',
          minWidth: 100,
          showOverflowTooltip: true
        },
        {
          prop: 'create_time',
          label: '上报时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 160,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 130,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.reload()
    })
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      let query = {}
      if (row) {
        query.id = row.id
      }
      this.$router.push({
        path: '/road-traffic/disease/edit',
        query: query
      })
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/disease/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      this.$confirm('确定要删除选中的数据吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/disease/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({type: 'success', message: res.data.msg});
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    setShowBatch() {
      this.$confirm('确定要把选中的数据设置为可见吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/disease/setShow', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({type: 'success', message: res.data.msg});
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    }
  }
}
</script>

<style scoped>
</style>
